package com.xyj.week01;

/**
 * 有序数组二分查找--找到某个数
 */
public class T7 {

    public static void main(String[] args) {
        int [] nums=new int[]{1,2,3,4,5,5,6};
        System.out.println(findOneIndex(nums,6));
        System.out.println(findOneIndex(nums,0));
    }

    //二分查找找一个数，找不到就返回-1
    public static int findOneIndex(int[] nums, int number){
        int left =0;
        int right =nums.length-1;
        while (left<=right){
            int mid = left + ((right-left)>>1);
            if (nums[mid]==number){
                return mid;
            }else if (nums[mid]<number){
                //如果中点小于这个数，那么这个数就在右半边
                left=mid+1;
            }else if (nums[mid]>number){
                //如果中点大于这个数，那么这个数就在左半边
                right = mid-1;
            }
        }
        return -1;
    }
}
